import { createRouter, createWebHistory } from 'vue-router'
import LoginPage from '@/views/Login/LoginPage.vue'
import LayoutPage from '@/views/Layout/LayoutPage.vue'

import HomePage from '@/views/Home/HomePage.vue'
import CategoryPage from '@/views/Category/CategoryPage.vue'
import SubCategoryPage from '@/views/SubCategory/SubCategoryPage.vue'
import DetailPage from '@/views/Detail/DetailPage.vue'
import CartListPage from '@/views/CartList/CartListPage.vue'
import CheckoutPage from '@/views/Checkout/CheckoutPage.vue'
import PayPage from '@/views/Pay/PayPage.vue'
import MemberPage from '@/views/Member/MemberPage.vue'

import UserOrder from '@/views/Member/componets/UserOrder.vue'
import UserInfo from '@/views/Member/componets/UserInfo.vue'

// import { useUserStore } from '@/stores/user'
// import 'element-plus/theme-chalk/el-message.css'
// import { ElMessage } from 'element-plus'



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/login',
      name:'Login',
      component:LoginPage
    }
    ,
    {
      path:'/',
      name:'Layout',
      component:LayoutPage,
      children:[
        {
          path:'/',
          component:HomePage
        }
        ,
        {
          path:'/category/:id',
          component:CategoryPage
        },
        {
          path:'/category/sub/:id',
          component:SubCategoryPage
        },
        {
          path:'/detail/:id',
          component:DetailPage
        },
        {
          path:'/cartlist',
          component:CartListPage
        },
        {
          path:'/checkout',
          component:CheckoutPage
        },
        {
          path:'/pay',
          component:PayPage
        },
        {
          path:'/member',
          component:MemberPage,
          children:[
            {
              path:'/member/order',
              component:UserOrder
            },
            {
              path:'',
              component:UserInfo
            }
          ]
        }
      ]
    }
  ],
  scrollBehavior() {
    return { top: 0 }
  }
})

//  router.beforeEach(async (to) => {
//    const userStore = useUserStore()
//    if (
//      // 检查用户是否已登录
//      !userStore.userInfo.token &&
//      // ❗️ 避免无限重定向
//      to.name !== 'Login'
//    ) {
//      // 将用户重定向到登录页面
//      ElMessage(
//       {
//         type: 'error',
//         message: '请先登录'
//       }
//      )
//      return { name: 'Login' }
//    }
//  })

export default router
